from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel,
                             QComboBox, QPushButton, QMessageBox)


class SpecialKeyDialog(QDialog):
    def __init__(self, action=None, parent=None):
        super().__init__(parent)
        self.setWindowTitle("特殊按键")

        layout = QVBoxLayout()

        # 特殊按键选择
        key_layout = QHBoxLayout()
        key_layout.addWidget(QLabel("选择特殊按键:"))
        self.key_combo = QComboBox()
        self.key_combo.addItems([
            "enter", "tab", "esc", "backspace", "delete",
            "up", "down", "left", "right",
            "home", "end", "pageup", "pagedown",
            "f1", "f2", "f3", "f4", "f5", "f6",
            "f7", "f8", "f9", "f10", "f11", "f12",
            "space", "insert", "capslock", "numlock"
            ,"z","f"
        ])
        key_layout.addWidget(self.key_combo)
        layout.addLayout(key_layout)

        # 按钮
        button_layout = QHBoxLayout()
        ok_button = QPushButton("确定")
        ok_button.clicked.connect(self.accept)
        cancel_button = QPushButton("取消")
        cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(ok_button)
        button_layout.addWidget(cancel_button)
        layout.addLayout(button_layout)

        self.setLayout(layout)

        # 如果有现有动作，填充数据
        if action:
            index = self.key_combo.findText(action.get("key", ""))
            if index >= 0:
                self.key_combo.setCurrentIndex(index)

    def get_action(self):
        text = self.key_combo.currentText().strip()
        if not text:
            QMessageBox.warning(self, "警告", "请选择特殊按键")
            return None

        return {
            "type": "specialkey",
            "text": text,
            "desc": f"按下特殊键: {text}"
        }